package com.java2007.hotel.service.impl;

import com.java2007.hotel.constant.Constant;
import com.java2007.hotel.dao.FoodTableDao;
import com.java2007.hotel.dao.OrderDao;
import com.java2007.hotel.dao.impl.FoodTableDaoImpl;
import com.java2007.hotel.factory.BeanFactory;
import com.java2007.hotel.pojo.FoodTable;
import com.java2007.hotel.pojo.Order;
import com.java2007.hotel.service.FoodTableService;
import com.java2007.hotel.utils.JdbcUtils;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import org.junit.Test;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * @author csl
 * @version V1.0
 * @date 2021/01/21
 **/
public class FoodTableServiceImpl implements FoodTableService {

    private FoodTableDao foodTableDao = (FoodTableDao) BeanFactory.getBean(Constant.FOOD_TABLE_DAO_IMPL);

    private OrderDao orderDao = (OrderDao) BeanFactory.getBean(Constant.ORDER_DAO_IMPL);
    /**
     * 增加一个餐桌
     * @param foodTable
     * @return
     * @throws SQLException
     */
    @Override
    public int addFoodTable(FoodTable foodTable) throws SQLException {
        if (foodTableDao.selectFoodTableByName(foodTable.getName()) == null){
            return 1;
        }
        JdbcUtils.startTransaction();
        int i = 0;
        try {
            i = foodTableDao.addFoodTable(foodTable);
        } catch (SQLException e) {
            e.printStackTrace();
            JdbcUtils.rollBack();
        }finally {
            JdbcUtils.commit();
        }
        return i;

    }

    /**
     * 查询所有餐桌
     * @return
     * @throws SQLException
     */
    @Override
    public List<FoodTable> selectAllFoodTable() throws SQLException {
        List<FoodTable> foodTables = foodTableDao.selectAllFoodTable();
        for (FoodTable foodTable : foodTables) {
            if (foodTable.getReserveDate() != null){
                foodTable.setReserveDateStr(foodTable.getReserveDate().toString());
            }

        }
        return foodTables;
    }

    /**
     * 更新餐桌状态
     * @param status
     * @param id
     * @return
     * @throws SQLException
     */
    @Override
    public int updateFoodTableStatus(byte status , int id) throws SQLException {

        int temp  =  (status == 0? 1: 0);
        Date date = new Date();
        int i = 0;
        JdbcUtils.startTransaction();
        try {
            if (temp == 1){
                i = foodTableDao.updateFoodTableStatus((byte) temp, id,date, date);
            }else {
                i = foodTableDao.updateFoodTableStatus((byte) temp, id,null, date);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            JdbcUtils.rollBack();
            throw  new RuntimeException("服务器出错");
        }finally {
            JdbcUtils.commit();
        }
        return i;
    }

    /**
     * 根据id删除餐桌
     * @param id
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteFoodTableById(int id) throws SQLException {
        //结账后桌子被预定不能删
        FoodTable foodTable = foodTableDao.selectFoodTableById(id);
        if (foodTable != null){
            if (foodTable.getStatus() == 1){
                return 3;
            }
        }
        List<Order> orders = orderDao.selectByTableId(id);
        if (orders.size() != 0){
            for (int i = 0; i < orders.size(); i++) {
                if (orders.get(i).getStatus() == 1){
                    return 2;
                }
            }
        }
        JdbcUtils.startTransaction();
        int i = 0;
        try {
            i = foodTableDao.deleteFoodTableById(id);
        } catch (SQLException e) {
            e.printStackTrace();
            JdbcUtils.rollBack();
            throw new RuntimeException("服务器出错了");
        }finally {
            JdbcUtils.commit();
        }
        return i;
    }

    /**
     * 根据名字查询餐桌
     * @param name
     * @return
     * @throws SQLException
     */
    @Override
    public List<FoodTable> selectFoodTableByName(String name) throws SQLException {
        return foodTableDao.selectFoodTableByName(name);
    }

    @Override
    public List<FoodTable> selectFoodTableByNameAccurate(String name) throws SQLException {
        return foodTableDao.selectFoodTableByNameAccurate(name);
    }
    /**
     * 根据餐桌的状态查询餐桌
     * @param status
     * @return
     */
    @Override
    public List<FoodTable> selectFoodTableByStatus(Byte status) throws SQLException {
        return   foodTableDao.selectFoodTableByStatus(status);

    }

    /**
     * 测试
     */
//    @Test
//    public void test (){
//        FoodTableServiceImpl foodTableService = new FoodTableServiceImpl();
//        try {
//            int i = foodTableService.updateFoodTableStatus((byte) 1, 4);
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//
//
//    }
}
